package batch.import_user;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.log4j.*;

import java.util.ArrayList;
import java.sql.*;
import java.io.*;

import batch.common.db.ConnectionManager;

public class makeTempTableWithoutIdnPwnPostnAd
{
    private final String FILE_NAME  = "/home01/sfc/batch/import_user/data/ita_200610_Monclick.txt";
    private final String TABLE_NAME = "G_ITA_IMPORT_TEMP_2";

    //arrStrData[0] : email
    //arrStrData[1] : firstname
    //arrStrData[2] : lastname
    //arrStrData[3] : gender
    //arrStrData[4] : birth
    //arrStrData[5] : street and number
    //arrStrData[6] : town
    //arrStrData[7] : zipcode

    public static void main(String[] args)
    {
        Logger.getLogger("import_user.makeTempTableWithoutIdnPwnPostnAd").info("( * ) Start.");

        makeTempTableWithoutIdnPwnPostnAd obj = new makeTempTableWithoutIdnPwnPostnAd();
        obj.init();

        Logger.getLogger("import_user.makeTempTableWithoutIdnPwnPostnAd").info("( * ) End.");
    }


    public void init(){
        Connection conn = null;

        BufferedReader bfrdForFile = null;

        int iRE = 0;

        ArrayList params = new ArrayList();

        String strTemp = "";
        StringBuffer sQuery = new StringBuffer()
                             .append("INSERT INTO " + TABLE_NAME + "( SEQ       \n")
                             .append("                              , EMAIL     \n")
                             .append("                              , FNAME     \n")
                             .append("                              , LNAME     \n")
                             .append("                              , GENDER    \n")
                             .append("                              , BIRTH     \n")
                             .append("                              , ADDRESS   \n")
                             .append("                              , POSTCODE) \n")
                             .append("                        VALUES( ?         \n")
                             .append("                              , ?         \n")
                             .append("                              , ?         \n")
                             .append("                              , ?         \n")
                             .append("                              , ?         \n")
                             .append("                              , ?         \n")
                             .append("                              , ?         \n")
                             .append("                              , ?)        \n");

        Logger.getLogger("import_user.makeTempTableWithoutIdnPwnPostnAd").info(FILE_NAME);

        try{
            bfrdForFile = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "ISO-8859-1"));

            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);

            QueryRunner qRunner = new QueryRunner();

            for(int i=0; (strTemp = bfrdForFile.readLine()) != null; i++) {
                String arrStrData[] = StringUtils.splitPreserveAllTokens(strTemp, "\t");

                for(int l=0;l<arrStrData.length;l++){
                    arrStrData[l] = StringUtils.stripToEmpty(arrStrData[l]);
                }

                params.clear();
                params.add(new Long(i + 1)                                             );  //SEQ
                params.add(arrStrData[0]                                               );  //EMAIL
                params.add(arrStrData[1]                                               );  //FNAME
                params.add(arrStrData[2]                                               );  //LNAME
                params.add(StringUtils.defaultIfEmpty(arrStrData[3], "M").toUpperCase());  //GENDER
                params.add(arrStrData[4]                                               );  //BIRTHDAY
                params.add(StringUtils.replace(arrStrData[5], "\"", "")
                         + " "
                         + StringUtils.replace(arrStrData[6], "\"", "")                );  //ADDRESS
                params.add(arrStrData[7]                                               );  //POSTCODE

                try{
                    iRE = qRunner.update(conn, sQuery.toString(), params.toArray());
                }
                catch(SQLException ex){
                    ex.printStackTrace();
                    Logger.getLogger("import_user.makeTempTableWithoutIdnPwnPostnAd").info(ex.toString());
                }

                if(i % 500 == 0){
                    Logger.getLogger("import_user.makeTempTableWithoutIdnPwnPostnAd").info("here we are == " + i);
                }
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
            Logger.getLogger("import_user.makeTempTableWithoutIdnPwnPostnAd").info(ex.toString());
        }
        finally {
            if (bfrdForFile != null) try { bfrdForFile.close(); } catch (Exception se) {}
            if (conn != null) try { conn.close(); } catch (Exception se) {}
        }
    }
}
